//
//  WRGestureIndicator.m
//  WRGesturePassword
//
//  Created by 王然 on 16/6/27.
//  Copyright © 2016年 wangran. All rights reserved.
//

#import "WRGestureIndicator.h"
#import "WRGestureClickView.h"

#define BaseCircleTag (800)     // tag基数（请勿修改）

@interface WRGestureIndicator()

@property (nonatomic , strong) NSMutableArray * clickVArr;

@property (nonatomic , strong) UIColor * normalColor;

@property (nonatomic , strong) UIColor * selectColor;

@end

@implementation WRGestureIndicator

- (instancetype)initWithFrame:(CGRect)frame withNormalC:(UIColor *)normalC withSelectColor:(UIColor *)selectC
{
    self = [super initWithFrame:frame];
    if (self) {
        self.normalColor = normalC;
        self.selectColor = selectC;
        [self setupCircles];
    }
    return self;
}
- (void)setupCircles
{
    //初始化9个按钮原点
    NSArray * numArr = @[@1,@2,@3,@4,@5,@6,@7,@8,@9];
    self.clickVArr = [NSMutableArray arrayWithCapacity:numArr.count];
    for (int index = 0; index < numArr.count; index ++) {
        
        CGFloat clickVX = (index % 3) * ( CircleMargin + CircleRediusDouble );
        CGFloat clickVY = (index / 3) * ( CircleMargin + CircleRediusDouble );
        WRGestureClickView * clickV = [[WRGestureClickView alloc]initWithFrame:CGRectMake( clickVX, clickVY, CircleRediusDouble, CircleRediusDouble)];
        [clickV reloadNormallColor:self.normalColor withSelelctColor:self.selectColor withWrongColor:nil];
        [clickV setBackgroundColor:[UIColor clearColor]];
        clickV.stateType = View_Click_Type_RightAll;
        clickV.tag = index + BaseCircleTag + 1; // tag从基数+1开始,
        [self addSubview:clickV];
        [self.clickVArr addObject:clickV];
    }
}
- (void) setPasswordString:(NSString *)string
{
    for (WRGestureClickView * clickV in self.clickVArr) {
        [clickV setSelected:NO];
    }
    for (int i=0; i<string.length; i++) {
        NSRange range = NSMakeRange(i, 1);
        NSNumber* number = [NSNumber numberWithInt:[string substringWithRange:range].intValue-1]; // 数字是1开始的
        WRGestureClickView * clickV = self.clickVArr[number.intValue];
        [clickV setSelected:YES];
    }
}

@end
